//
//  AdaptiveMoreButton.m
//  HotSearch
//
//  Created by 王勇 on 16/4/21.
//  Copyright © 2016年 Brave. All rights reserved.
//

#import "AdaptiveMoreButton.h"
#define SCREENW  ([UIScreen mainScreen].bounds.size.width)

@interface AdaptiveMoreButton()

/** 最后高度 */
@property (nonatomic ,assign) CGFloat heightf;

@end

@implementation AdaptiveMoreButton


- (UIView *)adaptiveMoreButton:(NSArray *)stringsArray Font:(CGFloat)font Column:(int)colum addToView:(UIView *)view
{
    if (colum ==1) return nil;
    UIView *subView         = [UIView new];
    subView.frame           = view.frame;
    NSArray *stringArray    = [[NSArray alloc] initWithArray:stringsArray];
    NSArray *stringWidthArr = [[NSArray alloc] initWithArray:[self stringWidth:stringsArray Font:font]
];
    // 布局
    for (int i = 0; i < stringArray.count; i++) {
        
        UIButton *btn       = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTitle:stringArray[i] forState:UIControlStateNormal];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.font = [UIFont systemFontOfSize:font];
        btn.backgroundColor = [UIColor colorWithRed:0.000 green:1.000 blue:0.502 alpha:1.000];

        //计算
        CGFloat strWidth    = [stringWidthArr[i] floatValue];//每个宽
        CGSize textRealSize = CGSizeMake(strWidth, self.btnHeight);
        // | 1 2 3 |
        CGFloat frame       = subView.frame.size.width;
        CGFloat ColuWidth   = (frame - colum*(textRealSize.width)-20)/(colum-1);//列宽
        CGFloat RowHeight   = self.rowHeight;//行
        CGFloat buttonX     = 10+i%colum*(ColuWidth+textRealSize.width);
        CGFloat buttonY     = 100+i/colum*(textRealSize.height+ RowHeight);
        btn.frame           = CGRectMake(buttonX,buttonY, textRealSize.width, textRealSize.height);
        [subView addSubview:btn];

      if(i == (stringArray.count-1)){// 获取最后一个按钮高度
                   
                    self.heightf = buttonY+textRealSize.height;
         }
    }
    return subView;
}
- (NSArray *)stringWidth:(NSArray *)strArray Font:(CGFloat)font
{
   
    NSMutableArray *widthArr = [NSMutableArray arrayWithCapacity:strArray.count];
    [strArray enumerateObjectsUsingBlock:^(NSString *str, NSUInteger idx, BOOL * _Nonnull stop) {
//               NSLog(@"%@ ------ %ld",str,idx);
        // 获取每个字符串长度
        CGSize textMaxSize  = CGSizeMake(MAXFLOAT,SCREENW);
        CGSize textRealSize = [self sizeWithFont:font maxSize:textMaxSize string:str];
        // 长度存入数组
        [widthArr addObject:[NSString stringWithFormat:@"%f",textRealSize.width]];
    }];
    return widthArr;
}
- (CGSize)sizeWithFont:(CGFloat)font maxSize:(CGSize)maxSize string:(NSString *)str
{
    UIFont *fonts       = [UIFont systemFontOfSize:font];
    NSDictionary *attrs = @{NSFontAttributeName : fonts};
    return [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
- (CGFloat)floatHeight{


    return self.heightf;
}
@end
